package com.usian.admin.controller;

import com.usian.admin.service.impl.AdChannelServiceImpl;
import com.usian.apis.admin.admin.AdChannelControllerApi;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理" ,tags = "channel", description = "频道管理API")
public class AdChannelController implements AdChannelControllerApi {

    @Autowired
//    @Resource(name = "AdChannelServiceImpl")
    private AdChannelServiceImpl channelService;

    @PostMapping("/list")
    @ApiOperation(value = "查询频道信息",notes = "根据名称分页查询频道列表")
    @ApiImplicitParam(name = "dto",type ="ChannelDto",value = "查询名称")
    @Override
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto dto){
        return channelService.findByNameAndPage(dto);
    }

    @Override
    @PostMapping("/save")
    @ApiOperation(value = "新增频道信息",notes = "新增频道信息")
    @ApiImplicitParam(name = "channel",type ="AdChannel",value = "新增频道")
    public ResponseResult save(@RequestBody AdChannel channel) {
        if (channel == null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        channel.setCreatedTime(new Date());
        channelService.save(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    @PostMapping("/update")
    @ApiOperation(value = "更新频道信息",notes = "更新频道信息")
    @ApiImplicitParam(name = "channel",type ="AdChannel",value = "更新频道")
    public ResponseResult update(@RequestBody AdChannel adChannel) {
        return channelService.update(adChannel);
    }

    @Override
    @GetMapping("/del/{id}")
    @ApiOperation(value = "删除频道信息",notes = "删除频道信息")
    @ApiImplicitParam(name = "id",type ="Integer",value = "删除频道")
    public ResponseResult deleteById(@PathVariable Integer id) {
        return channelService.deleteById(id);
    }

    @GetMapping("/channels")
    @Override
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }


//    public static void main(String[] args) {
////        String gensalt = BCrypt.gensalt();//这个是盐  29个字符，随机生成
////        System.out.println(gensalt);
////        String password = BCrypt.hashpw("123456", gensalt);  //
//        boolean checkpw = BCrypt.checkpw("123456","$2a$10$D1vMe0ck3YSfGuXj2k2BxO61m4goDbim.UQLPC8.wdwslYYWrNf.C");// 根据盐对密码进行加密
//        System.out.println(checkpw);//加密后的字符串前29位就是盐
//    }
}